// pages/home-music/index.js
import {getBannerList,getSongMenu} from "../../service/api_music"
import { querySelector} from "../../utils/query-rect"
import debounce from "../../utils/debounce"
import {store} from "../../store/index"
const debounceQuery = debounce(querySelector)
Page({

    /**
     * 页面的初始数据
     */
    data: {
       bannerList:[],
       swiperHeight:0,
       hotList:[],
       hotMenuList:[],
       recommendMenuList:[],

       newRanking:{},
       newSongRankings:[],
       
       originSongRanking:{},
       originSongRanking:[],

       upRanking:{},
       upSongRanking:[],
       isPlaying: false,
       playAnimState: "paused",
       currentSong:{}
    },
    //跳转
    handleInputClick(){
      wx.navigateTo({
        url: '../seach-detail/index',
      })
    },
    //图片加载获取高度
    handleImageload(){
    //console.log("图片加载完成")
    debounceQuery(".image").then((res)=>{
        this.setData({swiperHeight:res.height})
     })
    
    },
    //推荐详情页
    handleSongs(event){
     // console.log(event)
      wx.navigateTo({
        url: `../../pages/detail-songs/index?songsName=${event.currentTarget.dataset.songsname}&type=rank`
      })
    },
    
  handlePlayBarClick: function() {
    wx.navigateTo({
      url: '../../pages/music-player/index?id=' + this.data.currentSong.id,
    })
  },
  handlePlayBtnClick: function(event) {
    store.dispatch("changeMusicPlayStatusAction", !this.data.isPlaying)
    // Propagation 繁殖
    // event.stopPropagation()
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
       getBannerList(1).then((res)=>{
        // console.log(res.banners)
         this.setData({bannerList:res.banners})
       })
      store.dispatch("getTopListAction")
      store.onState("hotList",(res)=>{
           if(!res.tracks) return
            //console.log(res.tracks)
            this.setData({hotList:res.tracks.slice(0,6)})
      })

      //全部歌单
      getSongMenu().then((res)=>{
        //console.log(res)
        this.setData({hotMenuList:res.playlists})
      })

      //推荐歌单
      getSongMenu("华语").then((res)=>{
        //console.log(res)
        this.setData({recommendMenuList:res.playlists})
      })
      //榜单
      store.dispatch("getRankingsAction")
      store.onState("newRanking",(res)=>{
        if(!res.tracks) return
        //console.log(res)
         this.setData({newRanking:res,newSongRankings:res.tracks.slice(0,3)})
      })
      //
      store.onState("originRanking",(res)=>{
        if(!res.tracks) return
        //console.log(res)
         this.setData({originRanking:res,originSongRanking:res.tracks.slice(0,3)})
      })
      //
      store.onState("upRanking",(res)=>{
        if(!res.tracks) return
       // console.log(res)
         this.setData({upRanking:res,upSongRanking:res.tracks.slice(0,3)})
      })
      // 2.播放器监听
    store.onStates(["currentSong", "isPlaying"], ({currentSong, isPlaying}) => {
      if (currentSong) this.setData({ currentSong })
      if (isPlaying !== undefined) {
        this.setData({ 
          isPlaying, 
          playAnimState: isPlaying ? "running": "paused" 
        })
      }
    })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})